package com.riccardocardin.structured.concurrency.application;

import com.riccardocardin.structured.concurrency.service.GitHubUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

import static com.riccardocardin.structured.concurrency.model.Model.GitHubUser;
import static com.riccardocardin.structured.concurrency.model.Model.UserId;

public class GitHubUserPlainConcurrentAppService implements GitHubUserApplicationService {

    private static final Logger LOGGER = LoggerFactory.getLogger("GitHubApp");

    private final GitHubUserService gitHubUserService;

    public GitHubUserPlainConcurrentAppService(GitHubUserService gitHubUserService) {
        this.gitHubUserService = gitHubUserService;
    }

    @Override
    public GitHubUser findGitHubUser(UserId userId) throws InterruptedException, ExecutionException {
        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
            var user = executor.submit(() -> gitHubUserService.findUser(userId));
            var repositories = executor.submit(() -> gitHubUserService.findRepositories(userId));
            return new GitHubUser(user.get(), repositories.get());
            //throw new RuntimeException("Something went wrong");
        }
    }
}
